from matplotlib import pyplot as plt
import numpy as np

X = np.linspace(0,1,21)
#print(X)
#After 1, 2, and 10 time steps, the Crank-Nicolson method with r = 1 gives results as follows.
U = [0, 7.63105e-06, 3.05242e-05, 0.000114466, 0.000427339, 0.00159489, 0.00595222, 0.022214, 0.0829038, 0.309401, 0.154701, 0.309401, 0.0829038, 0.022214, 0.00595222, 0.00159489, 0.000427339, 0.000114466, 3.05242e-05, 7.63105e-06, 0];
plt.plot(X,U);
plt.savefig("CN11.png")
plt.show()
U = [0, 7.28537e-05, 0.000260891, 0.000848612, 0.00267569, 0.00814481, 0.023524, 0.0621422, 0.136189, 0.150998, 0.2302, 0.150998, 0.136189, 0.0621422, 0.023524, 0.00814481, 0.00267569, 0.000848612, 0.000260891, 7.28537e-05, 0];
plt.plot(X,U);
plt.savefig("CN12.png")
plt.show()
U = [0, 0.00730964, 0.0153286, 0.0245894, 0.0352904, 0.0471821, 0.0595412, 0.0712348, 0.0809364, 0.087364, 0.0896232, 0.087364, 0.0809364, 0.0712348, 0.0595412, 0.0471821, 0.0352904, 0.0245894, 0.0153286, 0.00730964, 0];
plt.plot(X,U);
plt.savefig("CN13.png")
plt.show()

#Crank-Nicolson with r = 2 gives results as follows.
U = [0, 0.000132214, 0.000396642, 0.00105771, 0.00277649, 0.00727177, 0.0190388, 0.0498446, 0.130495, 0.341641, -0.105573, 0.341641, 0.130495, 0.0498446, 0.0190388, 0.00727177, 0.00277649, 0.00105771, 0.000396642, 0.000132214, 0];
plt.plot(X,U);
plt.savefig("CN21.png")
plt.show()
U = [0, 0.000918129, 0.00248996, 0.00575847, 0.01267, 0.0266986, 0.0528823, 0.0938706, 0.12904, 0.03226, 0.284458, 0.03226, 0.12904, 0.0938706, 0.0528823, 0.0266986, 0.01267, 0.00575847, 0.00248996, 0.000918129, 0];
plt.plot(X,U);
plt.savefig("CN22.png")
plt.show()
U = [0, 0.00901871, 0.0179214, 0.0265658, 0.0347738, 0.0423749, 0.0491481, 0.0542905, 0.0595776, 0.0602349, 0.0636514, 0.0602349, 0.0595776, 0.0542905, 0.0491481, 0.0423749, 0.0347738, 0.0265658, 0.0179214, 0.00901871, 0];
plt.plot(X,U);
plt.savefig("CN23.png")
plt.show()

#The BTCS with r = 1 gives results as follows.
U = [0, 6.6107e-05, 0.000198321, 0.000528856, 0.00138825, 0.00363588, 0.0095194, 0.0249223, 0.0652476, 0.17082, 0.447214, 0.17082, 0.0652476, 0.0249223, 0.0095194, 0.00363588, 0.00138825, 0.000528856, 0.000198321, 6.6107e-05, 0];
plt.plot(X,U);
plt.savefig("BTCS11.png")
plt.show()
U = [0, 0.000295639, 0.000820811, 0.00196847, 0.00455575, 0.0103105, 0.02274, 0.04839, 0.0975076, 0.178885, 0.268328, 0.178885, 0.0975076, 0.04839, 0.02274, 0.0103105, 0.00455575, 0.00196847, 0.000820811, 0.000295639, 0];
plt.plot(X,U);
plt.savefig("BTCS12.png")
plt.show()
U = [0, 0.00659918, 0.0139151, 0.0225586, 0.032906, 0.044945, 0.0581231, 0.0712723, 0.0827086, 0.0905751, 0.093388, 0.0905751, 0.0827086, 0.0712723, 0.0581231, 0.044945, 0.032906, 0.0225586, 0.0139151, 0.00659918, 0];
plt.plot(X,U);
plt.savefig("BTCS13.png")
plt.show()

#The collocation method in Example 11.258 with r = 1 gives results as follows.
U = [0, 7.63105e-06, 3.05242e-05, 0.000114466, 0.000427339, 0.00159489, 0.00595222, 0.022214, 0.0829038, 0.309401, 0.154701, 0.309401, 0.0829038, 0.022214, 0.00595222, 0.00159489, 0.000427339, 0.000114466, 3.05242e-05, 7.63105e-06, 0];
plt.plot(X,U);
plt.savefig("CO11.png")
plt.show()
U = [0, 1.49969e-05, 5.94569e-05, 0.000219648, 0.000800566, 0.00287439, 0.0100662, 0.033714, 0.103362, 0.254841, 0.188081, 0.254841, 0.103362, 0.033714, 0.0100662, 0.00287439, 0.000800566, 0.000219648, 5.94569e-05, 1.49969e-05, 0];
plt.plot(X,U);
plt.savefig("CO12.png")
plt.show()
U = [0, 0.000538398, 0.00161544, 0.0041407, 0.00981694, 0.0214591, 0.0425783, 0.0751003, 0.114878, 0.148757, 0.16135, 0.148757, 0.114878, 0.0751003, 0.0425783, 0.0214591, 0.00981694, 0.0041407, 0.00161544, 0.000538398, 0];
plt.plot(X,U);
plt.savefig("CO13.png")
plt.show()

U = [0, 6.99029e-05, 0.000229295, 0.00067807, 0.00196274, 0.00558065, 0.0154592, 0.0410406, 0.100922, 0.210423, 0.247162, 0.210423, 0.100922, 0.0410405, 0.015459, 0.0055803, 0.00196208, 0.000677681, 0.000234655, 6.61976e-05, 0];
plt.plot(X,U);
plt.savefig("CO31.png")
plt.show()
U = [0, 0.000525763, 0.0014971, 0.00365975, 0.00845873, 0.0185316, 0.037907, 0.0705772, 0.115085, 0.15624, 0.173933, 0.15624, 0.115085, 0.0705767, 0.0379059, 0.0185294, 0.00845475, 0.00365811, 0.0015328, 0.000498576, 0];
plt.plot(X,U);
plt.savefig("CO32.png")
plt.show()
U = [0, 0.00888742, 0.0179234, 0.0271777, 0.0365777, 0.0458744, 0.0546477, 0.0623531, 0.0684051, 0.0722794, 0.0736128, 0.0722754, 0.068397, 0.0623409, 0.0546317, 0.0458566, 0.0365675, 0.0272308, 0.0183976, 0.00845418, 0];
plt.plot(X,U);
plt.savefig("CO33.png")
plt.show()
#===============================================================
#BTCS and the collocation method in Example 11.258 with r = 1/2h
U = [0, 0.00428057, 0.00898919, 0.0145967, 0.0216639, 0.0308976, 0.0432209, 0.0598664, 0.0824985, 0.11338, 0.1556, 0.11338, 0.0824985, 0.0598664, 0.0432209, 0.0308976, 0.0216639, 0.0145967, 0.00898919, 0.00428057, 0];
plt.plot(X,U);
plt.savefig("BTCS21.png")
plt.show()
U = [0, 0.00666058, 0.0135592, 0.0209147, 0.0289021, 0.0376133, 0.0469961, 0.0567563, 0.0662056, 0.0740256, 0.0779101, 0.0740256, 0.0662056, 0.0567563, 0.0469961, 0.0376133, 0.0289021, 0.0209147, 0.0135592, 0.00666058, 0];
plt.plot(X,U);
plt.savefig("BTCS22.png")
plt.show()
U = [0, 0.00173074, 0.00341894, 0.00502311, 0.00650377, 0.00782447, 0.00895262, 0.00986039, 0.0105253, 0.010931, 0.0110674, 0.010931, 0.0105253, 0.00986039, 0.00895262, 0.00782447, 0.00650377, 0.00502311, 0.00341894, 0.00173074, 0];
plt.plot(X,U);
plt.savefig("BTCS23.png")
plt.show()
U = [0, 0.00473813, 0.0104239, 0.0181944, 0.0296038, 0.046934, 0.0736509, 0.115098, 0.179565, 0.279945, -0.563687, 0.279945, 0.179565, 0.115098, 0.0736509, 0.046934, 0.0296038, 0.0181944, 0.0104239, 0.00473813, 0];
plt.plot(X,U);
plt.savefig("CO21.png")
plt.show()
U = [0, 0.0070978, 0.0151395, 0.0250674, 0.0377445, 0.0536779, 0.0723089, 0.090403, 0.0986191, 0.0744569, -0.0319003, 0.0744569, 0.0986191, 0.090403, 0.0723089, 0.0536779, 0.0377445, 0.0250674, 0.0151395, 0.0070978, 0];
plt.plot(X,U);
plt.savefig("CO22.png")
plt.show()
U = [0, 0.00716338, 0.0141482, 0.0207791, 0.0268877, 0.032319, 0.0369373, 0.0406327, 0.0433238, 0.0449575, 0.0455051, 0.0449575, 0.0433238, 0.0406327, 0.0369373, 0.032319, 0.0268877, 0.0207791, 0.0141482, 0.00716338, 0];
plt.plot(X,U);
plt.savefig("CO23.png")
plt.show()

#===============================================================
#FTCS with r = 1/2, 1
U = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0];
plt.plot(X,U);
plt.savefig("FTCS01.png")
plt.show()
U = [0, 0, 0, 0, 0, 0, 0, 0, 0.25, 0, 0.5, 0, 0.25, 0, 0, 0, 0, 0, 0, 0, 0];
plt.plot(X,U);
plt.savefig("FTCS02.png")
plt.show()
U = [0, 0, 0.00976563, 0, 0.0439453, 0, 0.117188, 0, 0.205078, 0, 0.246094, 0, 0.205078, 0, 0.117188, 0, 0.0439453, 0, 0.00976563, 0, 0];
plt.plot(X,U);
plt.savefig("FTCS03.png")
plt.show()

U = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0];
plt.plot(X,U);
plt.savefig("FTCS11.png")
plt.show()
U = [0, 0, 0, 0, 0, 0, 0, 0, 1, -2, 3, -2, 1, 0, 0, 0, 0, 0, 0, 0, 0];
plt.plot(X,U);
plt.savefig("FTCS12.png")
plt.show()
U = [0, -10, 55, -210, 615, -1452, 2850, -4740, 6765, -8350, 8953, -8350, 6765, -4740, 2850, -1452, 615, -210, 55, -10, 0];
plt.plot(X,U);
plt.savefig("FTCS13.png")
plt.show()

#===============================================================
#the 1-stage Gauss-Legendre RK method in Example 11.227 with r = 1, 1/2h
U = [0, 7.63105e-06, 3.05242e-05, 0.000114466, 0.000427339, 0.00159489, 0.00595222, 0.022214, 0.0829038, 0.309401, 0.154701, 0.309401, 0.0829038, 0.022214, 0.00595222, 0.00159489, 0.000427339, 0.000114466, 3.05242e-05, 7.63105e-06, 0];
plt.plot(X,U);
plt.savefig("GLRK11.png")
plt.show()
U = [0, 7.28537e-05, 0.000260891, 0.000848612, 0.00267569, 0.00814481, 0.023524, 0.0621422, 0.136189, 0.150998, 0.2302, 0.150998, 0.136189, 0.0621422, 0.023524, 0.00814481, 0.00267569, 0.000848612, 0.000260891, 7.28537e-05, 0];
plt.plot(X,U);
plt.savefig("GLRK12.png")
plt.show()
U = [0, 0.00730964, 0.0153286, 0.0245894, 0.0352904, 0.0471821, 0.0595412, 0.0712348, 0.0809364, 0.087364, 0.0896232, 0.087364, 0.0809364, 0.0712348, 0.0595412, 0.0471821, 0.0352904, 0.0245894, 0.0153286, 0.00730964, 0];
plt.plot(X,U);
plt.savefig("GLRK13.png")
plt.show()
U = [0, 0.00473813, 0.0104239, 0.0181944, 0.0296038, 0.046934, 0.0736509, 0.115098, 0.179565, 0.279945, -0.563687, 0.279945, 0.179565, 0.115098, 0.0736509, 0.046934, 0.0296038, 0.0181944, 0.0104239, 0.00473813, 0];
plt.plot(X,U);
plt.savefig("GLRK21.png")
plt.show()
U = [0, 0.0111968, 0.0227378, 0.0346567, 0.0462292, 0.055206, 0.0564505, 0.0395246, -0.0155355, -0.145529, 0.583394, -0.145529, -0.0155355, 0.0395246, 0.0564505, 0.055206, 0.0462292, 0.0346567, 0.0227378, 0.0111968, 0];
plt.plot(X,U);
plt.savefig("GLRK22.png")
plt.show()
U = [0, 0.00148077, 0.00277063, 0.00362619, 0.00393601, 0.00442993, 0.00782218, 0.0176494, 0.0212907, -0.0921518, 0.165242, -0.0921518, 0.0212907, 0.0176494, 0.00782218, 0.00442993, 0.00393601, 0.00362619, 0.00277063, 0.00148077, 0];
plt.plot(X,U);
plt.savefig("GLRK23.png")
plt.show()
#===============================================================

#U = [0,7.63105e-06, 3.05242e-05, 0.000114466, 0.000427339, 0.00159489, 0.00595222, 0.022214, 0.0829038, 0.309401, 0.154701, 0.309401, 0.0829038, 0.022214, 0.00595223, 0.00159494, 0.000427509, 0.000115098, 3.28853e-05, 1.64426e-05 ,0]
#plt.plot(X,U);
#plt.savefig("test.png")
#plt.show()
